;(function($){
	$.fn.pos_sel_fwy = function(opts){
		var el = this;
		var opt = {
			unUnames : [],//不显示的用户
			fwy : "",
			multi:true,
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		var t = this;
		this.initHmtl();
		this.initGrid();
		this.bindEvent();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//关闭
			el.find("#close-dialog,#modalBtnClose").unbind("click").click(function(){
				param.closeBack();
				el.modal("hide");
				el.empty();
			})
			
			//确定按钮
			el.find("#modalBtnOk").unbind("click").click(function(){
				var outJson = []
				var grid_selector = el.find('#modalGridTable_pos_sel_fwy');
				if(param.multi){
					var ids = grid_selector.jqGrid('getGridParam','selarrrow');
					$(ids).each(function(k,v){
						var rowData = grid_selector.jqGrid('getRowData',v);
						outJson.push({"uname":rowData.uname,"describ":rowData.describ});
					});
					param.dataBack(outJson);
				}else{
					var rowid = grid_selector.jqGrid('getGridParam','selrow');
					var rowdata = grid_selector.jqGrid('getRowData',rowid);
					param.dataBack(rowdata);
				}
			});
			
			//查询按钮
			el.find("#modalBtnSearch").unbind("click").click(function(){
				t.setGridValue();
			});
			
			//回车事件
			el.find("#benefitForm").unbind("keydown").keydown(function(e){
				if(e.keyCode==13){
					t.setGridValue();
				}
			})
			
			//筛选服务员自己
			el.find("#serverSelf").unbind("change").change(function(){
				t.setGridValue();
			})
		},
		initHmtl : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			this.el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static");
			var html = 	'<div class="modal-dialog" style="width:800px;">'+
							'<div class="modal-content">'+
								'<div class="modal-header no-padding">'+
									'<div class="table-header">'+
										'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML">'+
											'<span class="white">&times;</span>'+
										'</button>'+
										'<span id="modal-h4">'+
											$.i18n.propJsp("服务员")+
										'</span>&nbsp;&nbsp;&nbsp;&nbsp;'+
									'</div>'+
								'</div>'+
								'<div class="modal-body">'+
									'<div class="widget-box row" style="border:1px solid orange;">'+
										
										'<form id="benefitForm" class="form-horizontal col-sm-10" onsubmit="return false;" style="margin-top:10px;">'+
											
											'<div class="col-sm-2 no-padding">'+
												'<label style="margin-top:6px;">'+
													'<input class="ace" type="checkbox" name="sqlType" id="sqlType"/>'+
													'<span class="lbl">'+$.i18n.propJsp("编号模糊")+'</span>'+
												'</label>'+
											'</div>'+
									
											'<div class="col-sm-4 no-padding">'+
												'<div class="col-sm-4 no-padding">'+
													'<label class="col-sm-12 control-label no-padding-left" style="margin-bottom: 12px;">'+$.i18n.propJsp("编号")+'</label>'+
												'</div>'+
												'<div class="col-sm-8 no-padding">'+
													'<input id="uname" name="uname" class="input-md" type="text">'+
												'</div>'+
											'</div>'+
											
											'<div class="col-sm-4 no-padding">'+
												'<div class="col-sm-4 no-padding">'+
													'<label class="col-sm-12 control-label no-padding-left" style="margin-bottom: 12px;">'+$.i18n.propJsp("名称")+'</label>'+
												'</div>'+
												'<div class="col-sm-8 no-padding">'+
													'<input id="describ" name="describ" class="input-md" type="text">'+
												'</div>'+
											'</div>'+
											
											'<div class="col-sm-2 no-padding">'+
												'<label style="margin-top:6px;margin-left:45px;">'+
													'<input class="ace" type="checkbox" name="serverSelf" id="serverSelf"/>'+
													'<span class="lbl">'+$.i18n.propJsp("自己")+'</span>'+
												'</label>'+
											'</div>'+
										'</form>'+
										'<div class="col-sm-2" style="margin-top:10px;">'+
											'<div class="clearfix">'+
												'<div class="pull-right">'+
													'<div class="btn-group">'+
														'<button id="modalBtnSearch" class="btn btn-sm btn-info">'+
															'<i class="ace-icon fa fa-search"></i>'+
															$.i18n.propJsp("查询")+
														'</button>'+
													'</div>'+
												'</div>'+
											'</div>'+
										'</div>'+
									'</div>'+
									
									'<div class="widget-box">'+
										'<table id="modalGridTable_pos_sel_fwy"></table>'+
										'<div id="modalGridPage_pos_sel_fwy"></div>'+
									'</div>'+
								'</div>'+
								'<div class="modal-footer">'+
									'<div class="btn-group">'+
										'<button id="modalBtnOk" class="btn btn-sm btn-info">'+
											'<i class="ace-icon fa fa-check"></i>'+
											$.i18n.propJsp("确定")+
										'</button>'+
									'</div>'+
									'<div class="btn-group">'+
										'<button id="modalBtnClose" class="btn btn-sm btn-default">'+
											'<i class="ace-icon fa fa-times"></i>'+
											$.i18n.propJsp("关闭")+
										'</button>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</div>';
			this.el.html(html);
		},
		initGrid : function(){
			
			var el = this.el;
			var initc = this.getDs();
			var param = this.param;
			var t = this;
			var grid_selector = this.el.find("#modalGridTable_pos_sel_fwy");
			var pager_selector = this.el.find("#modalGridPage_pos_sel_fwy");
//			
			
			//构造表格
			grid_selector.jqGrid({
				url : $.HSAPI_AJAXURL + "/pos/front/hoguest/proceduregrid",
				datatype : "json",
				width :766,
				height : 300,
				postData : $.HSAPI_ASKJSONINFO( JSON.stringify(initc),'RetrieveJqGrid'),
				colNames : ["groupby","编号","名称"],
				colModel : [{
					name : "groupby",//"groupby",
					index : "groupby",
					width : 140,
					hidden : true,
				},{
					name : "uname",//"编号",
					index : "uname",
					width : 140,
					key : true,
				},{
					name : "describ",//"名称"
					index : "describ",
					width: 140,
				}],    	
				sortname : "groupby",
				sortorder : "asc",
				viewrecords : true,
				rowNum : "全部",
				rowList:[10,20,30],
				emptyrecords: $.i18n.propJsp("没有搜索到任何数据"),
				recordtext: $.i18n.propJsp("共计")+":{2}"+$.i18n.propJsp("条"),
				pager : pager_selector,
				altRows : true,  
				scrollrows:true,
				shrinkToFit : true,
				multiselect : param.multi,//true
				scroll: true,
			    onSelectRow:function(rowid){
			    	rowdata = grid_selector.jqGrid("getRowData",rowid);
		        },
	        	ondblClickRow: function(rowid) { 
	        		el.find("#selectionModalSave_Sales").trigger("click");
	        	},
	        	loadComplete : function(data) {
	        		var table = this;
					setTimeout(function(){
						updatePagerIcons(table);
					}, 0);
				}, 
			});
		},
		setGridValue : function(){
			var grid_selector = this.el.find("#modalGridTable_pos_sel_fwy");
			grid_selector.jqGrid("GridUnload");
			this.initGrid();
		},
		getDs : function(){
			var jons = [];
			var str = "1 = 1"
			var wherestring = {};
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//选服务员自己
			var uname = el.find("#uname").val();
			if(el.find("#serverSelf").is(":checked")){
				el.find("#uname").val("");
				el.find("#describ").val("");
				uname = param.fwy;
			}
			var describ = el.find("#describ").val();
			
			var sqlType = "="
			if(el.find("#sqlType").is(":checked")){
				sqlType = "like"
			}
			
			if(uname){
				if(sqlType=="like"){
					str +=  " and uname like '%"+uname+"%' " 
				}else{
					str +=  " and uname = '"+uname+"' " 
				}
			}
			
			if(describ){
				str +=  " and describ like '%"+describ+"%' " 
			}
			
			
			if(param.unUnames.length>0){
				var unUnames = param.unUnames.join(",");
				unUnames = "," + unUnames + ","
				str += " and charindex((','+uname+','),'"+unUnames+"')=0"
			}
			wherestring = {wherestring:["@@WS",str]}
			var where = {};
			where.where = {1:1};
			var fileds = {};
			fileds = {"fileds":["groupby","uname","describ"]};
			                           
			var table={};
			table.table={"table":"viewJqGrid_alinusers_pinyin"};				
			c= jQuery.extend({}, where,fileds,table,wherestring);
			return c;
		},
	}
	
	/*
	 * 私有函数
	 * 翻页栏
	 * 
	 */
	function updatePagerIcons(table) {
		var replacement = {
				"ui-icon-seek-first" : "ace-icon fa fa-angle-double-left bigger-140",
				"ui-icon-seek-prev" : "ace-icon fa fa-angle-left bigger-140",
				"ui-icon-seek-next" : "ace-icon fa fa-angle-right bigger-140",
				"ui-icon-seek-end" : "ace-icon fa fa-angle-double-right bigger-140"
		};
		$(".ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon").each(function() {
			var icon = $(this);
			var $class = $.trim(icon.attr("class").replace("ui-icon", ""));

			if ($class in replacement)
				icon.attr("class", "ui-icon " + replacement[$class]);
		});
	};
	
})(jQuery);